package ljl.alg.jianzhioffer.round3.medium;

public class _14_cuttingRope {

    /**
     * 1. n = ax, (x)^a = (x)^(n/x) = (x^(1/x))^n
     * 2. y = x^(1/x), lny = 1/x * lnx
     * 3. 1/y * y' = -1/x^2 * lnx + 1/x^2 = (1 - lnx) / x^2, y' = (1 - lnx) * y / x^2
     * 4. x = e
     * 5. x = 3 更大，其次是 2
     * */
    public int cuttingRope(int n) {
        if (n <= 3) return n - 1;
        int c3 = n / 3;
        int rem = n % 3;
        return switch (rem) {
            case 0 -> (int) Math.pow(3, c3);
            case 1 -> (int) (Math.pow(3, c3 - 1)) * 4;
            default -> (int) (Math.pow(3, c3)) * 2;
        };
    }
}
